﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF EXISTS (SELECT 1 FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[fn_has_access_permission]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [dbo].[fn_has_access_permission];
GO
CREATE FUNCTION [dbo].[fn_has_access_permission]
(
    @Class_ID   int,    -- the id of the class to validate (UDS_Class.ClassID)
    @UserID     int,    -- the name of the user
    @Act_ID     int     -- the id of the permission (UDS_Proc.Proc_ID)
)
RETURNS BIT
AS
BEGIN
    DECLARE @allowAccess bit;

    SET @allowAccess = dbo.fn_can_access_by_position(@Class_ID, @UserID, @Act_ID);
    IF ( @AllowAccess = 1 )
        RETURN 1;

    SET @allowAccess = dbo.fn_can_access_by_role(@Class_ID, @UserID, @Act_ID);
    IF ( @AllowAccess = 1 )
        RETURN 1;

    SET @allowAccess = dbo.fn_can_access_by_team(@Class_ID, @UserID, @Act_ID);
    IF ( @AllowAccess = 1 )
        RETURN 1;

    SET @allowAccess = dbo.fn_can_access_by_person(@Class_ID, @UserID, @Act_ID);
    IF ( @AllowAccess = 1 )
        RETURN 1;

    -- The user does not have the requested permission.
    RETURN 0;
    
END
GO